package com.avcdata.aoscustommade.utils;

import android.app.Activity;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import static android.content.Context.INPUT_METHOD_SERVICE;

/**
 * Created by c.c on 2017/2/26.
 * 软键盘控制类
 */
public class KeyBoardUtils {
    private KeyBoardUtils() {
        /* cannot be instantiated */
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    /**
     * 打开软键盘
     *
     * @param mEditText 输入框
     * @param mContext  上下文
     */
    public static void openKeybord(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext
                .getSystemService(INPUT_METHOD_SERVICE);
        imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
                InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 关闭软键盘
     *
     * @param mEditText 输入框
     * @param mContext  上下文
     */
    public static void closeKeybord(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext
                .getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
    }

    /**
     * 点击空白位置 隐藏软键盘
     */
    public static void closeTouchKeybord(Activity activity) {
        if (null != activity.getCurrentFocus ()) {
            InputMethodManager mInputMethodManager = (InputMethodManager) activity.getSystemService ( INPUT_METHOD_SERVICE );
            mInputMethodManager.hideSoftInputFromWindow ( activity.getCurrentFocus ().getWindowToken (), 0 );
        }
    }


}

